<?php
class CurrenciesController extends StoreAppController {

	var $name = 'StoreCurrencies';
	var $permissions = array(
		'add' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'change' => '*',
		'edit' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag')
	);	
	public function beforeFilter(){
		parent::beforeFilter();
//		$this->layout = 'cms';		
	}
	
	function index() {
		$this->StoreCurrency->recursive = 0;
		$this->set('storeCurrencies', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store currency', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('storeCurrency', $this->StoreCurrency->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->StoreCurrency->create();
			if ($this->StoreCurrency->save($this->data)) {
				$this->Session->setFlash(__d('store','The store currency has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store currency could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store currency', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->StoreCurrency->save($this->data)) {
				$this->Session->setFlash(__d('store','The store currency has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store currency could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->StoreCurrency->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store currency', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->StoreCurrency->delete($id)) {
			$this->Session->setFlash(__d('store','Store currency deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__d('store','Store currency was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function change(){
		if (!empty($this->data)) {
			$data = $this->StoreCurrency->read(null, $this->data['StoreCurrency']['selected_currency']);
			$this->Session->write('Store.selected_currency',$this->data['StoreCurrency']['selected_currency']);
			$this->Session->write('Store.selected_conversion',$data['StoreCurrency']['conversion']);
			$this->Session->write('Store.selected_exchange',$data['StoreCurrency']['exchange']);
		}
		$this->redirect($this->referer());		
	}
}
?>